package com.yd.mvc.baseController.interceptor;

import com.yd.app.util.BusinessException;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.util.AuthUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆拦截器
 */
public class MLoginInterceptor implements HandlerInterceptor {
	protected static final Logger LOGGER = LoggerFactory.getLogger(HandlerInterceptor.class);

	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		System.out.println("手机拦截器");
		HttpSession session = request.getSession();
		User u= (User) session.getAttribute("login_user");
		if(u!=null){
			return  true;
		}else{
			//System.out.println("未登录 手机拦截器 已关闭");
			//return  true;
			throw new BusinessException(401, "尚未登录", "no-login");
		}

	}

	public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception {
		AuthUtil.leave();
	}


}